package cn.wolfcode.web.controller;


import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.User;
import cn.wolfcode.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

/**
 *  自定义 登录
 */

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;

    /**
     * 登录
     */
    @ResponseBody
    @RequestMapping("/login")
    public JsonResult login(String username, String password, HttpSession session) throws IOException, ServletException {
        User user = userService.selectUserByName(username);
        if (user != null && user.getPassword().equals(password)){
            session.setAttribute("USER_IN_SESSION", user);
            return new JsonResult(true, "");
        }
        return new JsonResult(false, "账号或密码错误");
    }

    /**
     *  注册 检查用户名
     */
    @RequestMapping("/checkName")
    @ResponseBody
    public JsonResult checkName(String  username){
        if("".equals(username)){
            return new JsonResult(false, "用户名不能为空");
        }
        User user = userService.checkName(username);
        if (user == null){
            return new JsonResult(true,"名户名可以使用");
        }else {
            return new JsonResult(false, "用户名已被注册");
        }
    }

    /**
     *  注册
     */
    @RequestMapping("/register")
    @ResponseBody
    public JsonResult register(String  username, String password, HttpServletResponse response) throws IOException {
        if (password.trim().equals("")){
            return new JsonResult(false, "密码不能为空");
        }

        userService.register(username, password);
//        response.sendRedirect("/static/login2.html");  // 会被后面的覆盖
        return new JsonResult(true, "");
    }


    /**
     *  检查新密码
     */
    @RequestMapping("/checkNewPassword")
    @ResponseBody
    public JsonResult checkNewPassword(String newPassword, HttpSession session){
        if ("".equals(newPassword)){
            return new JsonResult(false, "密码不能为空");
        }
        User user =(User) session.getAttribute("USER_IN_SESSION");
        if (user != null && newPassword.equals(user.getPassword())){
            return new JsonResult(false, "新密码不能和原密码相同");

        }
        return new JsonResult(true, "");
    }

    /**
     *  修改密码
     */
    @RequestMapping("/updateKey")
    @ResponseBody
    public JsonResult updateKey(String newPassword,String surePassword,  HttpSession session){
        if ("".equals(surePassword) || !newPassword.equals(surePassword)){
            return new JsonResult(false, "密码不一致");
        }

        User user =(User) session.getAttribute("USER_IN_SESSION");
        if (user != null && newPassword.equals(user.getPassword())){
            return new JsonResult(false, "新密码不能和原密码相同");
        }else {
            Long id = user.getId();
            userService.updateKey(id, newPassword);

            return new JsonResult(true, "");
        }

    }

    /**
     *  安全退出
     */
    @RequestMapping("/out")
    public void out(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("USER_IN_SESSION");
        if (user != null){
            session.removeAttribute("USER_IN_SESSION");
            response.sendRedirect("/static/mytext/login2.html");
        }

        // =======
//        ArrayList
    }
}
